var baseId=GetQueryString('baseid')
/*****************************获取咨询师列表*********************/
    var getPsyerData={
        url:'Psychologist_Manage/psyList',
        fromwho:'ld',
        sendData:{
            p:'',
        }
    }
    getAjax(getPsyerData,
        function(data){
            if(data.result==1){
                var curData=data.data
                setPsyList(curData)
            }
        },
        function(data){
            console.log(data)
        }
    )
    function  setPsyList(curData){
        var html=''
        $.each(curData,function(k,v){
            html+=`
            <div class="mui-input-row mui-checkbox mui-left psyer-div">
                <label class="inputcontent clearfix">
                    <div class="img"><img src=${v.photo ? v.photo:"img/t.jpg" }/></div>
                    <div class="intro">
                        <span>${v.name}</span>
                        <span>-${v.tel}</span>
                        <span>(${v.address})</span>
                    </div>
                </label>
                <input name="pids" value="${v.id}" type="checkbox">
            </div>
            `
        })
        $('form.mui-input-group.psylist').html(html)
        
    }
/*****************************获取咨询师列表end*********************/
/*****************************查询列表*********************/ 
    $('#serachpsyer').on('keypress',function(){
        if(event.keyCode == 13){  
            var searchVal=$(this).val()
            var queryPsyerData={
                url:'Psychologist_Manage/psyList',
                fromwho:'ld',
                sendData:{
                    p:'',
                    queryPsy:searchVal
                }
            }
            getAjax(queryPsyerData,
                function(data){
                    if(data.result==1){
                        var data=data.data
                        setPsyList(data,false)
                        if(!$('.mui-collapse').hasClass('mui-active')){
                            $('.mui-collapse').addClass('mui-active')
                        }
                    }else{
                        mui.toast('你查找的内容不存在')
                    }
                },
                function(){
                    mui.toast('服务器出错')
                }
            )
        } 
    });
/*****************************查询列表end*********************/
/*****************************查询模版*********************/
var getOneTempData={
        url:'schedule_manage/sendLetter',
        fromwho:'ld',
        sendData:{
            base_msg_id:baseId,
            type:'entrust_psy'
        }
    }
    var letter_id,letter
    getAjax(getOneTempData,
        function(data){
            if(data.result==1){
                var tempData=decodeHtml(data.data.letter_file)
                letter_id=data.data.letter_id
                letter=data.data.letter
                var reg=/\{\{(.+?)\}\}/g
                var resultArr=[];
                while(result=reg.exec(tempData)){
                    if($.inArray(result[1],resultArr)==-1){
                        resultArr.push(result[1]);
                    }
                }
                var html='',html2=''
                if(resultArr.length>0){
                    var newArr=changetempname(resultArr)
                    console.log(newArr)
                    html+=`<form class="mui-input-group" id="sendFormTemp" style="color:#666">`
                    $.each(newArr,function(k,v){
                        html+=`
                            <div class="mui-input-row">
                                <label>${v}</label>
                            <input type="text" name="${k}" class="mui-input-clear" >
                            </div>
                        `
                    })
                    html+=`</form>`
                    $('#templist .mui-collapse-content').html(html)
                }else{
                    $('#templist').remove()
                }
                html2=`<button type="button" class="mui-btn mui-btn-primary" id="showtemp" style="margin: 20px auto;display: block;">预览函件</button>`
                $('.ul-ohter').after(html2)
                $(document).on('click','#showtemp',function(){
                    var showjson={}
                    $('#sendFormTemp').find('input').each(function(k,v){
                        var key=$(v).attr('name')
                        var val=$(v).val()
                        showjson[key]=val
                    })
                    var showhtml = template.render(tempData, showjson);
                    mui.alert(showhtml,'预览模版','','','div')
                })
            }else{
                
            }
        },
        function(data){
            console.log(data)
        }
    )
/*****************************获取模版end*********************/


















/*****************************提交*********************/
    $('#surepsyer').on('click',function(){
//      var content=$('.addResultBox').val()
        var ids={}
        var baseid=GetQueryString('baseid')
        $('form.mui-input-group').find('input[name="pids"]:checked').each(function(k,v){
            ids[k]=$(v).val()
        })
        if(!ids[0]){
            mui.toast('请选择你要指派的人')
            return false
        }
        $('#sendFormTemp').find('input').each(function(k,v){
            var key=$(v).attr('name')
            var val=$(v).val()
            letter[key]=val
        })
        var upCasePsyData={
            url:'Psychologist_Manage/appointPsy',
            fromwho:'ld',
            sendData:{
                id:ids,
                base_msg_id:baseid,
                content:'委托咨询师',
                letter:letter,
                letter_id:letter_id
            }
        }
        var waitingDialog
        mui.plusReady(function(){
            waitingDialog = plus.nativeUI.showWaiting("提交中");
        })
//      console.log(upCasePsyData)
        upAjax(upCasePsyData,
            function(data){
                mui.plusReady(function(){
                    waitingDialog.close();
                })
                if(data.result==1){
                    mui.toast('心理咨询指派成功')
                    setTimeout(function(){
                        document.location.href = localStorage.getItem('casebackurl')
                    },500)
                }else{
                    mui.toast(data.msg)
                }
            },
            function(data){
                console.log(data)
            }
        )
    })
/*****************************提交end*********************/